Skip to content

BUG: fix to_json on period #61203

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Conversation

xiaohuanlin
Copy link
Contributor

@xiaohuanlin xiaohuanlin commented Mar 30, 2025

@xiaohuanlin xiaohuanlin requested a review from WillAyd as a code owner March 30, 2025 17:35
@xiaohuanlin xiaohuanlin force-pushed the fix_55490 branch 2 times, most recently from 24e5d75 to dd9e2ce Compare March 30, 2025 19:40
@xiaohuanlin xiaohuanlin changed the title (WIP)BUG: fix to_json on period BUG: fix to_json on period Mar 30, 2025
@@ -568,6 +568,19 @@ cdef class BaseOffset:
out += ": " + ", ".join(attrs)
return out

def toDict(self) -> dict:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer not to implement this here but rather in the C extension code itself; this way of making something JSON serializable takes advantage of an obscure ultrajson method of allowing custom objects to be serialized, and it will be pretty slow compared to direct implementation in our C extension

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I agree with you on implementing it in C extension code

# GH55490
ser = Series(pd.period_range(start=2021, freq="Y", periods=1))
result = ser.to_json()
expected = (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this really the JSON representation that users want for a period?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found it interesting that the Period class actually has a freqstr property. This allows us to ignore freq when running to_json, which makes sense since freqstr already contains the necessary frequency information. Additionally, this approach helps resolve the issue.

@xiaohuanlin xiaohuanlin force-pushed the fix_55490 branch 2 times, most recently from 02739a2 to 07cf8a5 Compare May 12, 2025 11:51
@xiaohuanlin xiaohuanlin requested a review from WillAyd May 12, 2025 12:10
@xiaohuanlin
Copy link
Contributor Author

Does anyone else want to review it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BUG: .to_json() of periode_range gives OverflowError: Maximum recursion level reached
2 participants